package com.example.booksystem.biz;

import com.example.booksystem.bean.Member;
import com.example.booksystem.bean.MemberType;
import com.example.booksystem.dao.MemberDao;
import com.example.booksystem.dao.MemberTypeDao;

import java.util.List;

public class MemberBiz {
  MemberDao  memberDao = new MemberDao();
  MemberTypeDao typeDao = new MemberTypeDao();
  public int add(String name,String pwd,long typeId,double balance,String tel,String idNumber){
      return memberDao.add(name,pwd,typeId,balance,tel,idNumber);
  }
  public int modify(long id,String name,String pwd,long typeId,double balance,String tel,String idNumber){
      return memberDao.modify(id,name,pwd,typeId,balance,tel,idNumber);
  }
  public int remove(long id) throws Exception {
    //1.判断会员账号余额 >0 :提示不能删除
    Member member = getById(id);
    if(member.getBalance()>0){
      throw new Exception("此会员消费金额大于0,删除失败");
    }
    //2.有外键不能删除
    if(memberDao.exits(id)){
      throw new Exception("此会员有子信息,删除失败");
    }
    //3.删除
      return memberDao.remove(id);
  }
  public int modifyBalance(String idNumber,double amount){
      return memberDao.modifyBalance(idNumber,amount);
  }
  public List<Member> getAll(){
    MemberTypeDao typeDao = new MemberTypeDao();
    List<Member> members = null;
    members =  memberDao.getAll();
    for(Member member:members){
        //根据类型编号获取类型对象
        MemberType type = typeDao.getById(member.getTypeId());
        member.setType(type);
    }
    return members;
  }
  public Member getById(long id){
    Member member = memberDao.getById(id);
    MemberType memberType = typeDao.getById(member.getTypeId());
    member.setType(memberType);
    return member;
  }

  /**
   * 会员信息+会员对应的类型信息
   * @param idNumber
   * @return
   */
  public Member getByIdNumber(String idNumber){
    Member member = memberDao.getByIdNumber(idNumber);
    MemberType memberType = typeDao.getById(member.getTypeId());
    member.setType(memberType);
    return member;
  }

}
